﻿
window.onpageshow = function(event) {
	if (event.persisted) {
		console.log("???")
		$(function(){$(".cover").css("display","none")})
	}
};

var _ua = (function(u){
  return {
    Tablet:(u.indexOf("windows") != -1 && u.indexOf("touch") != -1 && u.indexOf("tablet pc") == -1) 
      || (u.indexOf("firefox") != -1 && u.indexOf("tablet") != -1),
    Mobile:(u.indexOf("windows") != -1 && u.indexOf("phone") != -1)
      || (u.indexOf("firefox") != -1 && u.indexOf("mobile") != -1)
  }
})(window.navigator.userAgent.toLowerCase());


var meta = document.createElement('meta');
meta.setAttribute('name', 'viewport');
if(_ua.Mobile){
	meta.setAttribute('content', 'width=device-width, initial-scale=1.0');
}else{
	meta.setAttribute('content', 'width=1140');
}
document.getElementsByTagName('head')[0].appendChild(meta);
if(!_ua.Mobile && !_ua.Tablet){
	$('html').addClass('pcState');
}

if(navigator.userAgent.indexOf('Android') > 0 && navigator.userAgent.indexOf('Mobile') > 0){
	$('html').addClass('android');
}
	
/*
 * jQuery throttle / debounce - v1.1 - 3/7/2010
 * http://benalman.com/projects/jquery-throttle-debounce-plugin/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function(b,c){var $=b.jQuery||b.Cowboy||(b.Cowboy={}),a;$.throttle=a=function(e,f,j,i){var h,d=0;if(typeof f!=="boolean"){i=j;j=f;f=c}function g(){var o=this,m=+new Date()-d,n=arguments;function l(){d=+new Date();j.apply(o,n)}function k(){h=c}if(i&&!h){l()}h&&clearTimeout(h);if(i===c&&m>e){l()}else{if(f!==true){h=setTimeout(i?k:l,i===c?e-m:e)}}}if($.guid){g.guid=j.guid=j.guid||$.guid++}return g};$.debounce=function(d,e,f){return f===c?a(d,e,false):a(d,f,e!==false)}})(this);

(function($){
  $.fn.scrollClass = function(config){
    var defaults = {};
    var config = $.extend(defaults, config);
    var target = this;

    function addAction(){
      var length = target.length;
      for(var i=0; i<length; i++){
        if(target.eq(i).hasClass('action')) continue;

        var in_position = target.eq(i).offset().top;
        var window_bottom_position = $(window).scrollTop() + $(window).height();
        if(in_position < window_bottom_position){
          target.eq(i).addClass('action');
        }
      }
    }
    addAction();

    $(window).on('scroll', $.throttle(250, function(){
      addAction();
    }));
    return target;
  };
} )(jQuery);

var myScroll;
$(window).load(function(){
	$('.target').delay(500).scrollClass();
	myScroll = new IScroll('.menuBox', {
		mouseWheel: true,
		click: true
	});
});

$(function(){
	var menuflag = 0;
	var closeflag = 0;
	$('#gHeader .menu a').mouseover(function(){
		if($(window).width() > 767 && menuflag == 0){
			$('.menuBox').stop().animate({'right':'0'},300);
			menuflag = 1;
			return false;
		}
	});
	
	$('.menuBox').mouseleave(function(){
		if($(window).width() > 767 && menuflag == 1 && closeflag == 0){
			$('.menuBox').stop().animate({'right':'-400px'},300,function(){
				menuflag = 0;
			});
			return false;
		}
	});
	
	$('#gHeader .menu a').click(function(){
		if($(window).width() < 768 && menuflag == 0){
			$('.menuBox').stop().animate({'right':'0'},300);
			$('.spCover').show();
			menuflag = 1;
		}
		return false;
	});
	$('.menuBox .close a').click(function(){
		if($(window).width() > 767){
			closeflag = 1;
			$('.menuBox').stop().animate({'right':'-400px'},300,function(){
				menuflag = 0;
				closeflag = 0;
			});
		}else{
			$('.menuBox').stop().animate({'right':'-90%'},300);
			$('.spCover').hide();
			menuflag = 0;
		}
		return false;
	});	
	
	$('.spCover').click(function(){
		$(this).hide();
		$('.menuBox').stop().animate({'right':'-90%'},300);
		menuflag = 0;
	});

	$(window).scroll(function(){
		if($(window).scrollTop() > 200){
			$('#gHeader').addClass('fix');
		}else{
			$('#gHeader').removeClass('fix');
		}
	}).trigger('scroll');
	
	$('#gNavi li a,.pageChange,#gHeader .logo a,.menuUl li a:not(.noChange),#main .topMenu a,#gFooter .language li a,#gFooter .fNavi li a:not(.noChange)').live('click',function(){
		if($(window).width() > 767){
			var href = $(this).attr('href');
			$('.topTarget,#main,#gFooter').addClass('animate').one('webkitTransitionEnd mozTransitionEnd MSTransitionEnd transitionend', function(){
				setTimeout(function(){
				  $('.topTarget,#main,#footer').removeClass('animate');
				},800);
			});
			if(menuflag == 1){
				$('.menuBox').fadeOut(function(){
					$(this).animate({'right':'-400px'},300);
					menuflag = 0;
				});
			}
	
			setTimeout(function(){
				window.location.href = href;
				setTimeout(function(){
					$('.cover').fadeOut(0);
				},800)
			},800);
			return false;
		}
	});

	$("div:not(.pop) img.lazy").lazyload({
		threshold : 1,
		effect : 'fadeIn',
		effect_speed: 500,
		failure_limit: 10,
		event: 'scroll',
		placeholder: '/img/common/lazy_dummy.png'
	});
	
	if($('.comNews').length){
		$('.comNews').each(function(){
			var slider = $(this);
			slider.find('.comNewsUl').slick({
				slidesToShow: 4,
				prevArrow: slider.find('.prev'),
				nextArrow: slider.find('.next'),
				touchThreshold: 20,
				responsive: [{
					breakpoint: 767,
					settings: {
						slidesToShow: 1,
						variableWidth: true,
						infinite: false,
					}
				}]
			})
		});
		$('.comNews .comNewsUl').on('beforeChange', function(event, slick, currentSlide, nextSlide){
			echo.render();
		});
	}
	
	if($('.comExhibition').length){
		var big = $('#main .comExhibition ul li').eq(0).children(' .photo').height();
		$(window).load(function(){
			$('#main .comExhibition ul li .photo').each(function(i){
				if(big < $(this).height()){
					big=$(this).height();
				}
			});
			$('#main .comExhibition ul li .photo').height(big);
		});
		
		$('.comExhibition').each(function(){
			var slider = $(this);
			var length = $(this).find('.sliderList li').length;
			slider.find('.sliderList').slick({
				autoplay: true,
				slidesToShow: length-1,
				slidesToScroll: 1,
				centerMode: true,
				arrows: true,
				variableWidth: true,
				pauseOnHover: false,
				speed: 300,
				autoplaySpeed: 5000,
				pauseOnFocus: false,
				touchThreshold: 20,
				prevArrow: slider.find('.prev'),
				nextArrow: slider.find('.next')
			});
		});
		
	}
	
	var pcflag,spflag;
	if($(window).width() > 767){
		pcflag = 1;
		spflag = 0;
	}else{
		pcflag = 0;
		spflag = 1;
	}
	
	$(window).resize(function(){
		if($(window).width() < 767){
			if(pcflag){
				setTimeout(function(){window.location.reload()},100);
				pcflag = 0;
				spflag = 1;
			}
		}else{
			if(spflag){
				setTimeout(function(){window.location.reload()},100);
				pcflag = 1;
				spflag = 0;
			}
		}
	});
	
	$(window).resize(function(){
		if($(window).width() > 767){
			if($(window).height() > $('.menuBox .menuContent').height() + 100){
				var h = ($(window).height() - $('.menuBox .menuContent').height() - 100)/2 + 50;
				$('.menuBox .menuContent').css({paddingTop:h,paddingBottom:h});
			}else{
				$('.menuBox .menuContent').css({paddingTop:'50px',paddingBottom:'50px'});
			}
		}else{
			if($(window).height() > $('.menuBox .menuContent').height() + 66){
				var h = ($(window).height() - $('.menuBox .menuContent').height() - 66)/2 + 33;
				$('.menuBox .menuContent').css({paddingTop:h,paddingBottom:h});
			}else{
				$('.menuBox .menuContent').css({paddingTop:'33px',paddingBottom:'33px'});
			}
		}
	}).trigger('resize');
	
	if($('.echo-img').length){
		echo.init({
			offset: 100,
			throttle: 10,
			callback: function (element, op) {
				if (op === 'load') {
					element.classList.add('loaded');
				} else {
					element.classList.remove('loaded');
				}
			}
		  });
	}
	if($('.object-fit-img').length){
		objectFitImages('img.object-fit-img');
	}
});


//page-scroller
$(function(){
    $('a[href*=#]:not([href=#],.fancybox)').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
				if($('#main').hasClass('index')){
					var targetOffset = $target.offset().top;
				}else{
					if($(window).width() > 767){
						var targetOffset = $target.offset().top - 95;
					}else{
						var targetOffset = $target.offset().top - 85;
					}
				}
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
	
	var localLink = window.location+'';
	if(localLink.indexOf("#") != -1){
		localLink = localLink.slice(localLink.indexOf("#")+1);
		if(localLink){
			if($('#main').hasClass('index')){
				$('html,body').animate({scrollTop: $('#'+localLink).offset().top}, 500);
			}else{
				if($(window).width() > 767){
					$('html,body').animate({scrollTop: $('#'+localLink).offset().top-60}, 500);
				}else{
					$('html,body').animate({scrollTop: $('#'+localLink).offset().top-50}, 500);
				}
			}
		}
	}
});

$(window).load(function(){
	setTimeout(function(){
		$(window).trigger('scroll');
		
		if(typeof(myFoo) == 'function'){
			setTimeout(function(){
				myFoo();
			},100);
		}
	},500);
})



// シェアボタン押下
function onClickShare(sns)
{
	// 今いるページのURL取得
	var url = location.href;
	if (sns === 'facebook')
	{
		window.open(
			'https://www.facebook.com/sharer/sharer.php?u='+url,
			'Facebook',
			'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600'
		);
	}
	else if (sns === 'twitter')
	{
		window.open(
			'https://twitter.com/intent/tweet?url='+url,
			'twitter',
			'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600'
		);
	}
}
